﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sheeps;
using System.Windows.Threading;

namespace SheepWpfInterface
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Game game;
        public DogBehaviorAlogorithm DogBehavior 
        {
            get
            {
                if (this.game == null)
                {
                    return null;
                }
                return this.game.DogAlgorithm;

            }
            set
            {
                this.game.DogAlgorithm = value;
            }
        }
        PasturePainter painter;

        private DispatcherTimer timer;

        public Window1()
        {
            InitializeComponent();
        }

        private DogBehaviorAlogorithm GetNewAlgorithm()
        {
            DogBehaviorAlogorithm alg = null;
            if (manualRadioButton.IsChecked.Value)
            {
                alg = new ManualOverrideDogBehavior();
            }
            else if (superGeniousRadioButton.IsChecked.Value)
            {
                alg = new SuperGeniusDogBehavior();
            }
            if (alg != null)
            {
                currentAlgorithmLabel.Content = alg.GetType().ToString();
            }
            return alg;
        }

        private void generateButton_Click(object sender, RoutedEventArgs e)
        {
            RandomSheepGenerator randSheepGen = new RandomSheepGenerator();
            randSheepGen.BorderWeight = Convert.ToDouble(borderSizeWeight.Text);

            var behavior = new MxSheepBehavior();
            behavior.RandomMoveWeight = Convert.ToDouble(this.sheepRandomMove.Text);
            this.game = new Game(this.GetNewAlgorithm(), behavior, randSheepGen, 
                paintCanvas.ActualWidth, paintCanvas.ActualHeight);
            this.DogBehavior = this.game.DogAlgorithm;
            this.game.GenerateSheep(Convert.ToInt32(countTextBox.Text));
            this.painter = new PasturePainter(this.game.State, paintCanvas);
            this.painter.InitializeCanvas();
            
        }

        private void WindowDisplay_Loaded(object sender, RoutedEventArgs e)
        {
            this.timer = new DispatcherTimer();
            this.timer.Interval = TimeSpan.FromSeconds(0.15);
            timer.IsEnabled = false;
            timer.Tick += new EventHandler(timer_Tick);
        }

        void timer_Tick(object sender, EventArgs e)
        {

            ManualOverrideDogBehavior manualDogBehavior = this.DogBehavior as ManualOverrideDogBehavior;
            if (manualDogBehavior != null)
            {
                return;
            }

            this.game.NextStep();
            this.painter.UpdateCanvas();
        }


        private void paintCanvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            var pos = e.GetPosition((IInputElement)sender);
            ManualOverrideDogBehavior manualDogBehavior = this.DogBehavior as ManualOverrideDogBehavior;
            if (manualDogBehavior != null)
            {
                manualDogBehavior.X = (int)pos.X;
                manualDogBehavior.Y = (int)pos.Y;
            }

            this.game.NextStep();
            this.painter.UpdateCanvas();
            this.painter.UpdateCursor(pos.X, pos.Y);
        }

        private void paintCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            var pos = e.GetPosition((IInputElement)sender);
            if (game == null)
            {
                return;
            }
            this.painter.UpdateCursor(pos.X, pos.Y);
        }

        private void switchAlgorithmButton_Click(object sender, RoutedEventArgs e)
        {
            this.DogBehavior = this.GetNewAlgorithm();

        }

        private void activateTimerButton_Click(object sender, RoutedEventArgs e)
        {
            this.timer.IsEnabled = !timer.IsEnabled;
        }
    }
}
